package com.example.dzbsh.controller;

import com.example.dzbsh.model.Fujian;
import com.example.dzbsh.model.GuifanluoshiQingkuang;
import com.example.dzbsh.service.*;
import com.example.dzbsh.util.JsonResult;
import com.example.dzbsh.util.PageInfo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.*;

@RestController
@RequestMapping("guifanluoshi_qingkuang")
public class GuifanluoshiQingkuangController {

    @Resource
    GuifanluoshiQingkuangService ddService;
    @Resource
    ZhenbieQingkuangService ddZhenbieQingkuangService;
    @Resource
    RenyuanService ddRenyuanService;
    @Autowired
    JiashuService ddJiashuService;
    @Resource
    FujianService ddFujianService;


    private Logger logger = LoggerFactory.getLogger(getClass());

    // 获得不同状态的规范数据的数量
    @RequestMapping("findGuifannum")
    public JsonResult<?> findGuifanNum(){
        try {
            int mmAllNum = ddService.countByGuifanleixing("");//所有
            int mmWeishangchuan = ddService.countWeishangchuan(); // 未上传
            int mmGanbutuichuNum = ddService.countByGuifanleixing("1");// 干部本人
            int mmJiashutuichuNum = ddService.countByGuifanleixing("0");//退出经商办

            Map<String, Object> mmReturnMap = new HashMap<String, Object>();
            mmReturnMap.put("allnum", mmAllNum);
            mmReturnMap.put("weishangchuannum", mmWeishangchuan);
            mmReturnMap.put("ganbutuichunum", mmGanbutuichuNum);
            mmReturnMap.put("jiashutuichunum", mmJiashutuichuNum);
            return JsonResult.getSuccessResult(mmReturnMap);
        }catch (Exception e){
            e.printStackTrace();
            logger.error("zhenbieqingkuang/findGuifannum:error",e);
            return JsonResult.getErrorResult("zhenbieqingkuang/findGuifannum:error " + e.getMessage());
        }
    }

    // 规范汇总表查询使用
    @RequestMapping("find_custom_by_deptid")
    public JsonResult<?> findCustomByDeptId(@RequestParam("deptid") String ppDeptId,@RequestParam("renyuanleibie") String ppRenyuanleibieId,
                                            @RequestParam("jigoumingchengma") String ppJigoumingchengma, @RequestParam("paixu") String ppPaixu,
                                            @RequestParam("pageindex") int ppPageIndex, @RequestParam("pagesize") int ppPageSize){
        try {
            List<Map<String,Object>> mmList = ddRenyuanService.findCustomRenyuanByDeptIdByGuifan(ppDeptId,ppRenyuanleibieId,ppJigoumingchengma,ppPaixu,ppPageIndex,ppPageSize); // 查询该单位规范的干部信息列表
            List<Map<String,Object>> mmReturnList = new ArrayList<>();
            for (int i = 0; i < mmList.size(); i++) {
                int mmRenyuanHebingshu = 0;
                Map<String,Object> mmMap = mmList.get(i);
                if (mmMap!=null){
                    List<Map<String,Object>> mmReturnJiashuList = new ArrayList<>();

                    String mmRenyuanId = mmMap.get("renyuanid") == null ? "" : mmMap.get("renyuanid").toString();

                    List<Map<String,Object>> mmJiashuList = ddJiashuService.findByRenyuanidByGuifan(mmRenyuanId); // 查询该领导规范的家属集合
                    for (int j = 0; j < mmJiashuList.size(); j++) {
                        int mmjiashuhebing = 0;
                        Map<String,Object> mmJiashuMap = mmJiashuList.get(j);

                        if (mmJiashuMap!=null){

                            String mmJiashuId = mmJiashuMap.get("jiashuid") == null ? "" : mmJiashuMap.get("jiashuid").toString();

                            List<Map<String,Object>> mmZhenbieqingkuangList = ddZhenbieQingkuangService.findByJiashuIdByGuifan(mmJiashuId);// 查询该家属的甄别情况
                            List<GuifanluoshiQingkuang> mmGuanfanqingkuangList = ddService.findByJiashuId(mmJiashuId); // 查询该家属的规范落实

                            if (mmZhenbieqingkuangList.size()>0){
                                mmjiashuhebing = mmZhenbieqingkuangList.size();
                                mmRenyuanHebingshu = mmRenyuanHebingshu+mmjiashuhebing;
                            }else{
                                mmRenyuanHebingshu = mmRenyuanHebingshu+1;
                            }
                            mmJiashuMap.put("zhenbielist",mmZhenbieqingkuangList);
                            mmJiashuMap.put("guifanlist",mmGuanfanqingkuangList);

                            mmJiashuMap.put("hebinghang",mmjiashuhebing);
                            mmReturnJiashuList.add(mmJiashuMap);
                        }


                    }
                    mmMap.put("hebinghang",mmRenyuanHebingshu);
                    mmMap.put("jiashuguifanlist",mmReturnJiashuList);
                }
                mmReturnList.add(mmMap);
            }
            Integer mmCount = ddRenyuanService.getCustomCountByDeptIdByGuifan(ppDeptId,ppRenyuanleibieId,ppJigoumingchengma);
            PageInfo mmPageInfo = new PageInfo(ppPageIndex, ppPageSize, mmCount);

            Map<String, Object> mmReturnMap = new HashMap<String, Object>();
            mmReturnMap.put("RenyuanList", mmReturnList);
            mmReturnMap.put("PageInfo", mmPageInfo);
            return JsonResult.getSuccessResult(mmReturnMap);
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.getErrorResult("guifanluoshi_qingkuang/find_custom_by_deptid:error " + e.getMessage());
        }
    }

    @RequestMapping("find_guifanluoshi_by_deptid_and_name")
    public JsonResult<?> findByDeptIdAndName(
            @RequestParam("deptid") String ppDeptId, @RequestParam("jiashuname") String ppJiashuName, @RequestParam("startindex") int ppStartIndex, @RequestParam("pagesize") int ppPageSize){
        try {
            return JsonResult.getSuccessResult(ddService.findGuifanluoshiByDeptIdAndName(ppDeptId,ppJiashuName,ppStartIndex,ppPageSize));
        }catch(Exception e) {
            e.printStackTrace();
            logger.error("GuifanluoshiQingkuangController->findJingshangchaheduibiByDeptIdAndName:"+e.getMessage());
            return JsonResult.getErrorResult("查询失败！");
        }
    }



    @RequestMapping("find_by_jiashuid")
    public JsonResult<?> findByJiashuId(
            @RequestParam("jiashuid") String ppJiashuId){
        try {
            return JsonResult.getSuccessResult(ddService.findByJiashuId(ppJiashuId));
        }catch(Exception e) {
            e.printStackTrace();
            logger.error("GuifanluoshiQingkuangController->findByJiashuId:"+e.getMessage());
            return JsonResult.getErrorResult("查询失败！");
        }
    }

    @RequestMapping("find_valid_by_jiashuid")
    public JsonResult<?> findValidByJiashuId(
            @RequestParam("jiashuid") String ppJiashuId){
        try {

            List<Map<String,Object>> mmList = ddService.findValidByJiashuId(ppJiashuId);
            List<Map<String,Object>> mmResultList = new ArrayList<>();
            for (int i = 0; i < mmList.size(); i++) {
                Map<String,Object> mmMap = mmList.get(i);
                if (mmMap!=null){
                    String mmZhengmingcailiaoId = mmMap.get("zhengmingcailiaoid")==null?"":mmMap.get("zhengmingcailiaoid").toString();
                    String mmGuifanluoshiId = mmMap.get("id")==null?"":mmMap.get("id").toString();

                    List<Fujian> mmFujianList = ddFujianService.findByGuifanluoshiidAndZhengmingcailiaoId(mmGuifanluoshiId,mmZhengmingcailiaoId);

                    mmMap.put("FujianList",mmFujianList);

                    mmResultList.add(mmMap);
                }
            }

            return JsonResult.getSuccessResult(mmResultList);
        }catch(Exception e) {
            e.printStackTrace();
            logger.error("GuifanluoshiQingkuangController->findValidByJiashuId:"+e.getMessage());
            return JsonResult.getErrorResult("查询失败！");
        }
    }

    @RequestMapping("find_one")
    public JsonResult<?> findOne(
            @RequestParam("id") String ppId){
        try {
            return JsonResult.getSuccessResult(ddService.findOne(ppId));
        }catch(Exception e) {
            e.printStackTrace();
            logger.error("GuifanluoshiQingkuangController->findOne:"+e.getMessage());
            return JsonResult.getErrorResult("查询失败！");
        }
    }




}
